package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "livrable")
public class Livrable implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;
	
	@Column(name = "comment")
	private String comment;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "categorie_id", nullable = false)
	private Categorie categorie;
	
	@OneToMany(mappedBy = "livrable", fetch = FetchType.LAZY)
	private List<LivrableChampValue> values = new ArrayList<LivrableChampValue>();
	
	// FIXME c'est un OneToMany !!!! sauf evolution plus tard
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "livrable_has_livraison", joinColumns = { @JoinColumn(name = "livrable_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "livraison_id", nullable = false, updatable = false) })
	private Set<Livraison> livraisons = new HashSet<Livraison>();
	// FIXME ça sert vraiment à quelque chose ??? le modèle est faux
	
	
	public Livrable() {
	}
	
	public Livrable(int id) {
		this.id = id;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}	

	public Categorie getCategorie() {
		return this.categorie;
	}

	public void setCategorie(Categorie categorie) {
		this.categorie = categorie;
	}

	public List<LivrableChampValue> getValues() {
		return values;
	}

	public void setValues(List<LivrableChampValue> values) {
		this.values = values;
	}

	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Set<Livraison> getLivraisons() {
		return this.livraisons;
	}

	public void setLivraisons(Set<Livraison> livraisons) {
		this.livraisons = livraisons;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

    @Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Livrable other = (Livrable) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}
    
    @Override
	public String toString() {
		return "Livrable [id=" + id + ", values=" + values
				+ ", comment=" + comment + ", categorie=" + categorie
				+ "]";
	}

}
